package com.lujianfei.netease_android_mvp.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.lujianfei.netease_android_mvp.R
import com.lujianfei.netease_android_mvp.bean.MainBean

class MainAdapter : BaseAdapter() {

    private val mData = ArrayList<MainBean>()

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        var holder: ViewHolder
        var retView: View

        if(convertView == null){
            retView = LayoutInflater.from(parent?.context).inflate(R.layout.adapter_main_item, null)
            holder = ViewHolder(retView)
            retView.tag = holder
        } else {
            holder = convertView.tag as ViewHolder
            retView = convertView
        }

        val item = getItem(position)
        holder.title.text = item.title
        holder.summary.text = item.summary
        return retView
    }

    override fun getItem(position: Int): MainBean {
        return mData[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return mData.size
    }

    fun setData(data : List<MainBean>) {
        mData.clear()
        mData.addAll(data)
        notifyDataSetChanged()
    }
    class ViewHolder(private var viewItem: View) {
        var title: TextView = viewItem.findViewById(R.id.title) as TextView
        var summary: TextView = viewItem.findViewById(R.id.summary) as TextView
    }

}